package com.hollowlatte.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hollowlatte.common.utils.Result;
import com.hollowlatte.manage.entity.Course;
import com.hollowlatte.manage.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Hollow Latte
 * @since 2021-05-05
 */
@RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
@Tag(name = "CourseController", description = "课程管理")
@CrossOrigin
@RestController
@RequestMapping("/course")
public class CourseController {
    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }


    @RequiresRoles("teacher")
    @Operation(summary = "通过id查找课程")
    @GetMapping()
    public Result findById(@RequestParam Long courseId) {
        Course course = courseService.getById(courseId);

        return Result.ok().data("course", course);
    }


    @RequiresRoles("teacher")
    @Operation(summary = "分页查询")
    @GetMapping("pagination")
    public Result paging(@RequestParam Long current, @RequestParam Long size) {
        Page<Course> coursePage = new Page<>(current, size);
        courseService.page(coursePage);

        return Result.ok().data("pageInfo", coursePage);
    }


    @RequiresRoles("teacher")
    @Operation(summary = "多条件分页查询", description = "可以传入条件：课程名（模糊查询）、学期数")
    @PostMapping("pagination")
    public Result multiConditionPaging(@RequestParam Long current, @RequestParam Long size, @RequestBody(required = false) Course course) {
        Page<Course> coursePage = new Page<>(current, size);

        if (course == null) {
            courseService.page(coursePage, null);
            return Result.ok().data("pageInfo", coursePage);
        }
        String courseName = course.getCourseName();
        Integer term = course.getTerm();

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();

        if (!StringUtils.isBlank(courseName)) {
            courseQueryWrapper.like("course_name", courseName);
        }
        if (term != null) {
            courseQueryWrapper.eq("term", term);
        }

        courseService.page(coursePage, courseQueryWrapper);

        return Result.ok().data("pageInfo", coursePage);
    }


    @RequiresRoles("admin")
    @Operation(summary = "添加课程")
    @PostMapping("")
    public Result addCourse(@RequestBody Course course) {
        boolean affect = courseService.save(course);

        if (affect) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    @RequiresRoles("admin")
    @Operation(summary = "通过id修改课程")
    @PutMapping()
    public Result updateCourseById(@RequestBody Course course) {
        boolean affect = courseService.updateById(course);

        if (affect) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    @RequiresRoles("admin")
    @Operation(summary = "通过id删除课程")
    @DeleteMapping()
    public Result deleteCourseById(@RequestParam Long courseId) {
        boolean affect = courseService.removeById(courseId);

        if (affect) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}

